2000W / 48V LLC Battery-Charger  V1.00
xmc1_flash.c
Go to the documentation of this file.
1 
53 #include "xmc_flash.h"
54 
55 /*********************************************************************************************************************
56  * MACROS
57  ********************************************************************************************************************/
58 #if UC_FAMILY == XMC1
59 
60 /*********************************************************************************************************************
61  * ENUMS
62  ********************************************************************************************************************/
63 /* FLASH programming / erase options */
64 typedef enum FLASH_ACTION
65 {
66  FLASH_ACTION_IDLE = (uint32_t)0x00,
76 
77 
78 /*********************************************************************************************************************
79  * API IMPLEMENTATION
80  ********************************************************************************************************************/
81 
82 /*
83  * This API shall clear the ECC and VERIFICATION error status.
84  */
86 {
87  NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk | (uint16_t)NVM_NVMPROG_RSTECC_Msk);
88 }
89 
90 /*
91  * This API shall return the status of NVM.
92  */
93 uint32_t XMC_FLASH_GetStatus(void)
94 {
95  return NVM->NVMSTATUS;
96 }
97 
98 /*
99  * This API shall enable the the flash interrupt event.
100  */
101 void XMC_FLASH_EnableEvent(const uint32_t event_msk)
102 {
103  NVM->NVMCONF |= (uint16_t)event_msk;
104 }
105 
106 /*
107  * This API shall disable the the flash interrupt event.
108  */
109 void XMC_FLASH_DisableEvent(const uint32_t event_msk)
110 {
111  NVM->NVMCONF &= (uint16_t)(~(uint16_t)event_msk);
112 }
113 
114 void XMC_FLASH_ErasePage(uint32_t *address)
115 {
116  (void)XMC1000_NvmErasePage(address);
117 }
118 
119 void XMC_FLASH_ProgramVerifyPage(uint32_t *address, const uint32_t *data)
120 {
121  (void)XMC1000_NvmProgVerify(data, address);
122 }
123 
124 /* Write blocks of data into flash*/
125 void XMC_FLASH_WriteBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks, bool verify)
126 {
127  uint32_t word;
128  uint32_t block;
129 
130  XMC_ASSERT("XMC_FLASH_WriteBlocks: Starting address not aligned to Block",
131  ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
132 
133  /* Configure the continuous Write option command and reset the NVM error / verification status*/
134  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
135  NVM->NVMPROG |= (uint16_t)(NVM_NVMPROG_RSTVERR_Msk | NVM_NVMPROG_RSTECC_Msk);
136 
137  if (verify == true)
138  {
139  NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE_VERIFY;
140  }
141  else
142  {
143  NVM->NVMPROG |= (uint16_t)FLASH_ACTION_CONTINUOUS_WRITE;
144  }
145 
146  for (block = 0U; block < num_blocks; ++block)
147  {
148  for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
149  {
150  *address = *data;
151  data++;
152  address++;
153  }
154 
155  while (XMC_FLASH_IsBusy() == true)
156  {
157  }
158  }
159 
160  /* Stop continuous write operation */
161  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
162 }
163 
164 /* Erase flash pages */
165 void XMC_FLASH_ErasePages(uint32_t *address, uint32_t num_pages)
166 {
167  uint32_t page;
168 
169  XMC_ASSERT("XMC_FLASH_ErasePages: Starting address not aligned to Page",
170  ((uint32_t)address & FLASH_PAGE_ADDR_MASK) == 0U)
171 
172  for (page = 0U; page < num_pages; ++page)
173  {
174  (void)XMC1000_NvmErasePage(address);
175 
176  while (XMC_FLASH_IsBusy() == true)
177  {
178  }
179 
180  /* Increment the page address for the next erase */
181  address += XMC_FLASH_WORDS_PER_PAGE;
182 
183  }
184 
185 }
186 
187 /* Write multiple data blocks and verify the written data */
188 void XMC_FLASH_VerifyBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks)
189 {
190  uint32_t word;
191  uint32_t block;
192 
193  XMC_ASSERT("XMC_FLASH_VerifyBlocks: Starting address not aligned to Block",
194  ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
195 
196  /* Configure the Continuous write with verify option command and reset the NVM error / verification status*/
197  NVM->NVMPROG &= (uint16_t)~NVM_NVMPROG_ACTION_Msk;
198  NVM->NVMPROG |= (uint16_t)((uint16_t)NVM_NVMPROG_RSTVERR_Msk |
199  (uint16_t)NVM_NVMPROG_RSTECC_Msk |
201 
202  for (block = 0U; block < num_blocks; ++block)
203  {
204  for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
205  {
206  *address = *data;
207  data++;
208  address++;
209  }
210 
211  while (XMC_FLASH_IsBusy() == true)
212  {
213  }
214  }
215 
216  /* Stop continuous verify operation */
217  NVM->NVMPROG &= (uint16_t)(~(uint16_t)NVM_NVMPROG_ACTION_Msk);
218 }
219 
220 /* Read data blocks from flash */
221 void XMC_FLASH_ReadBlocks(uint32_t *address, uint32_t *data, uint32_t num_blocks)
222 {
223  uint32_t word;
224  uint32_t block;
225 
226  XMC_ASSERT("XMC_FLASH_ReadBlocks: Starting address not aligned to Block",
227  ((uint32_t)address & FLASH_BLOCK_ADDR_MASK) == 0U)
228 
229  for (block = 0U; block < num_blocks; ++block)
230  {
231  for (word = 0U; word < XMC_FLASH_WORDS_PER_BLOCK; ++word)
232  {
233  *data = *address;
234  data++;
235  address++;
236  }
237  }
238 }
239 
240 /* Erase single sector */
241 void XMC_FLASH_EraseSector(uint32_t *address)
242 {
243  XMC_ASSERT("XMC_FLASH_EraseSector: Starting address not aligned to Sector",
244  ((uint32_t)address & FLASH_SECTOR_ADDR_MASK) == 0U)
245  XMC_FLASH_ErasePages(address, XMC_FLASH_PAGES_PER_SECTOR);
246 }
247 
248 /* Program single page */
249 void XMC_FLASH_ProgramPage(uint32_t *address, const uint32_t *data)
250 {
251  XMC_FLASH_ProgramVerifyPage(address, data);
252 }
253 
254 #endif
void XMC_FLASH_EraseSector(uint32_t *address)
Definition: xmc1_flash.c:241
#define NVM_NVMPROG_RSTVERR_Msk
Definition: XMC1400.h:1970
#define XMC1000_NvmProgVerify
void XMC_FLASH_WriteBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks, bool verify)
Definition: xmc1_flash.c:125
void XMC_FLASH_VerifyBlocks(uint32_t *address, const uint32_t *data, uint32_t num_blocks)
Definition: xmc1_flash.c:188
void XMC_FLASH_ProgramPage(uint32_t *address, const uint32_t *data)
Definition: xmc1_flash.c:249
#define NVM
Definition: XMC1400.h:8816
void XMC_FLASH_ClearStatus(void)
Definition: xmc1_flash.c:85
uint32_t XMC_FLASH_GetStatus(void)
Definition: xmc1_flash.c:93
#define XMC1000_NvmErasePage
void XMC_FLASH_ReadBlocks(uint32_t *address, uint32_t *data, uint32_t num_blocks)
Definition: xmc1_flash.c:221
#define XMC_ASSERT(msg, exp)
Definition: xmc_common.h:132
void XMC_FLASH_ErasePage(uint32_t *address)
Definition: xmc1_flash.c:114
void XMC_FLASH_DisableEvent(const uint32_t event_msk)
Definition: xmc1_flash.c:109
void XMC_FLASH_ErasePages(uint32_t *address, uint32_t num_pages)
Definition: xmc1_flash.c:165
__STATIC_INLINE bool XMC_FLASH_IsBusy(void)
Definition: xmc_flash.h:258
enum FLASH_ACTION FLASH_ACTION_t
void XMC_FLASH_ProgramVerifyPage(uint32_t *address, const uint32_t *data)
Definition: xmc1_flash.c:119
void XMC_FLASH_EnableEvent(const uint32_t event_msk)
Definition: xmc1_flash.c:101
#define NVM_NVMPROG_ACTION_Msk
Definition: XMC1400.h:1968
#define NVM_NVMPROG_RSTECC_Msk
Definition: XMC1400.h:1972
FLASH_ACTION
Definition: xmc1_flash.c:64
#define NVM_NVMPROG_ACTION_Pos
Definition: XMC1400.h:1967